package cn.pet.service.manage.controller;

import cn.pet.service.manage.auth.MyAuthenticationProvider;
import cn.pet.service.manage.entity.PetUser;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.LoginDTO;
import cn.pet.service.manage.entity.dto.UserDTO;
import cn.pet.service.manage.service.PetUserService;
import cn.pet.service.manage.utils.SecurityUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zwr
 * @version 1.0
 * @description TODO
 * @date 2023/2/18 11:40
 */
@RestController
@RequestMapping("/manage/member")
@Slf4j
@AllArgsConstructor
public class MemberController {

    private final PetUserService petUserService;

    private final MyAuthenticationProvider authenticationProvider;

    /**
     * 查看会员列表
     * @param userDTO
     * @return
     */
    @GetMapping("list")
    public Object list(UserDTO userDTO){
        if (log.isDebugEnabled()){
            log.debug("查询会员列表：第{}页，每页条数：{}",userDTO.getPageNum(),userDTO.getPageSize());
        }
        //构造分页构造器
//        Page pageInfo = new Page(userDTO.getPage(),userDTO.getPageSize());
        PageHelper.startPage(userDTO.getPageNum(),userDTO.getPageSize());
        //构造条件构造器
        LambdaQueryWrapper<PetUser> queryWrapper = new LambdaQueryWrapper();
        //添加过滤条件
        queryWrapper.eq(PetUser::getUserType,1);
        queryWrapper.like(StringUtils.isNotEmpty(userDTO.getUsername()),PetUser::getUsername,userDTO.getUsername());
        //添加排序条件
        queryWrapper.orderByDesc(PetUser::getCreateTime);
        //执行查询
//        petUserService.page(pageInfo, queryWrapper);
        return Result.ok("success",new PageInfo<>(petUserService.list(queryWrapper)));
    }

    /**
     * 会员注册
     * @param petUser
     * @return
     */
    @PostMapping("register")
    public Object register(@RequestBody PetUser petUser){
        petUser.setStatus(0);
        petUser.setUserType(1);
        petUser.setPassword(SecurityUtil.encryptPassword(petUser.getPassword()));
        if (!petUserService.save(petUser)) {
            return Result.error("注册失败");
        }
        return Result.ok("注册成功");
    }

    /**
     * 会员登录
     * @param dto
     * @return
     */
    @PostMapping("login")
    public Object login(@RequestBody LoginDTO dto){
        Authentication authentication = authenticationProvider.authenticate(dto.getUsername(), dto.getPassword());
        Map<String,Object> result = new HashMap<>();
        result.put("accessToken",authentication.getCredentials());
        result.put("member",authentication.getPrincipal());
        return Result.ok("success",result);
    }

    /**
     * 获取会员信息
     * @return
     */
    @PostMapping("getMember")
    public Object getMember(){
        try {
            Integer userId = SecurityUtil.getUserId();
            return Result.ok("success",petUserService.getById(userId));
        }catch (Exception e) {
            return Result.ok("success");
        }
    }


}
